 /*
 * @name: 运费模版
 * @Author: ice
 * @Date: 2020-04-12
 * @Last Modified by: --
 * @Last Modified time: --
 */
<template>
    <div class="marketGoodFreight goodfreight">
        <!-- header -->
        <div class="header-nav">
            <div class="header-left">
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        模版名称:
                    </p>
                    <el-input
                        v-model="filterHead.keyword"
                        placeholder="请输入内容"
                        size="small"
                        clearable
                        suffix-icon="el-icon-search"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        模版状态:
                    </p>
                    <el-select
                        v-model="filterHead.template_status"
                        clearable
                        filterable
                        placeholder="请选择"
                        size="small"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in typeList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        计费方式:
                    </p>
                    <el-select
                        v-model="filterHead.freight_type"
                        clearable
                        filterable
                        placeholder="请选择"
                        size="small"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in modeList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
            </div>
            <div class="header-right">
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="()=>{
                            this.$router.push({
                                name: 'goodsFreightEdit'
                            })
                        }"
                    >
                        新增运费模版
                    </el-button>
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="goodfreight"
            @sort-change="handleSortChanged"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="template_name"
                label="模版名称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="freight_type"
                label="计费方式"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <p v-if="scope.row.freight_type == 'weight'">
                        按重量计费
                    </p>
                    <p v-if="scope.row.freight_type == 'num'">
                        按件计费
                    </p>
                </template>
            </el-table-column>
            <el-table-column
                prop="template_sort"
                label="排序号"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                sortable
                :width="100"
            />
            <!-- <el-table-column
                prop="freight_list"
                label="指定区域配送"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <p
                        @click="fnCheckList(scope.row)"
                        class="cursor-font"
                    >
                        查看
                    </p>
                </template>
            </el-table-column> -->
            <el-table-column
                prop="freight_list"
                label="首重(件)"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    {{ scope.row.freight_list[0].freight_num_start }}{{ scope.row.freight_type == 'weight' ? 'kg' : '件' }}
                </template>
            </el-table-column>
            <el-table-column
                prop="freight_list"
                label="首重(件)价格"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="110"
            >
                <template slot-scope="scope">
                    ¥{{ scope.row.freight_list[0].freight_fee_start }}
                </template>
            </el-table-column>
            <el-table-column
                prop="freight_list"
                label="续重(件)"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    {{ scope.row.freight_list[0].freight_num_other }}{{ scope.row.freight_type == 'weight' ? 'kg' : '件' }}
                </template>
            </el-table-column>
            <el-table-column
                prop="freight_list"
                label="续重(件)价格"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="110"
            >
                <template slot-scope="scope">
                    ¥{{ scope.row.freight_list[0].freight_fee_other }}
                </template>
            </el-table-column>
            <el-table-column
                prop="freight_list"
                label="邮费满额包邮"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="110"
            >
                <template slot-scope="scope">
                    <p v-if="scope.row.freight_list[0].freight_fee_none">
                        ¥{{ scope.row.freight_list[0].freight_fee_none }}
                    </p>
                    <p v-else>
                        --
                    </p>
                </template>
            </el-table-column>
            <el-table-column
                prop="delivery_type"
                label="不配送区域"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="100"
            >
                <template slot-scope="scope">
                    <div
                        v-if="scope.row.delivery_type == -1 && scope.row.delivery_list.length > 0"
                        style="display:flex"
                    >
                        <p
                            v-for="(item,index) in scope.row.delivery_list"
                            :key="index"
                        >
                            {{ item.name }} {{ (index == 0 && scope.row.delivery_list.length == 0) || (index == scope.row.delivery_list.length - 1) ? '' : ';' }}
                        </p>
                    </div>
                    <p v-else>
                        -
                    </p>
                </template>
            </el-table-column>
            <el-table-column
                prop="delivery_type"
                label="只配送区域"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="100"
            >
                <template slot-scope="scope">
                    <div
                        v-if="scope.row.delivery_type == 1 && scope.row.delivery_list.length > 0"
                        style="display:flex"
                    >
                        <p
                            v-for="(item,index) in scope.row.delivery_list"
                            :key="index"
                        >
                            {{ item.name }} {{ (index == 0 && scope.row.delivery_list.length == 0) || (index == scope.row.delivery_list.length - 1) ? '' : ';' }}
                        </p>
                    </div>
                    <p v-else>
                        -
                    </p>
                </template>
            </el-table-column>
            <el-table-column
                prop="freight_list"
                label="指定区域设置运费"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="140"
            >
                <template slot-scope="scope">
                    <p v-if="scope.row.freight_list.length > 1">
                        是
                    </p>
                    <p v-else>
                        否
                    </p>
                </template>
            </el-table-column>
            <el-table-column
                prop="template_status"
                label="状态"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.template_status"
                        :active-value="1"
                        :inactive-value="0"
                        @change="fnChangeStatus(scope.row)"
                    />
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="center"
                fixed="right"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnEditTemp(scope.row)"
                        style="padding-bottom:3px;"
                    >
                        编辑
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnRemoveTemp(scope.row)"
                        style="padding:0;margin-left:0"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- 配送区域列表 -->
        <el-dialog
            :visible.sync="dialogVisib"
            width="660px"
            :close-on-click-modal="false"
            class="dialodRegion"
        >
            <el-table
                :data="freight_list"
                height="100%"
                style="width: 100%;max-height:500px"
                border
            >
                <el-table-column
                    prop="freight_num_start"
                    :label="freight_type ? '首重(kg)' : '首件(件)'"
                    align="center"
                    show-overflow-tooltip
                />
                <el-table-column
                    prop="freight_fee_start"
                    :label="freight_type ? '首重价格(元)' : '首件价格(元)'"
                    align="center"
                    show-overflow-tooltip
                />
                <el-table-column
                    prop="freight_num_other"
                    :label="freight_type ? '续重(kg)' : '续件(件)'"
                    align="center"
                    show-overflow-tooltip
                />
                <el-table-column
                    prop="freight_fee_other"
                    :label="freight_type ? '续重价格(元)' : '续件价格(元)'"
                    align="center"
                    show-overflow-tooltip
                />
                <el-table-column
                    prop="freight_fee_none"
                    label="邮费满额包邮"
                    align="center"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                />
            </el-table>
        </el-dialog>

        <!-- /分页 -->
        <div class="pagination-wrapper clearfixNew">
            <el-pagination
                background
                :current-page.sync="page.now_page"
                :page-size="page.page_size"
                layout="total, prev, pager, next, jumper"
                :total="page.total_count"
                @current-change="getTableData"
            />
        </div>
    </div>
</template>

<script>
export default {
    mixins: [],
    components: {
    },
    data () {
        return {
            freight_type: '',//计费方式
            freight_list: [],//配送区域数据
            dialogVisib: false,//配送区域
            filterHead: {
                keyword: '',
                template_status: '',
                freight_type: ''
            },
            typeList: [{
                value: '',
                label: '全部'
            },{
                value: 1,
                label: '已显示'
            },{
                value: 0,
                label: '已隐藏'
            }],
            modeList: [{
                value: '',
                label: '全部'
            },{
                value: 'num',
                label: '按件计费'
            },{
                value: 'weight',
                label: '按重量计费'
            }],
            filterData: {
                sort_field: 'asc',
                sorttype: 'desc'
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: []
        };
    },
    filters: {},
    computed: {},
    watch: {},
    created(){
        this.$store.commit('header/SET_HEADER', [
            {title: '校园商城'},
            {title: '店铺管理'},
            {title: '运费模板'}
        ]);
    },
    mounted () {
        this.getTableData();
    },
    methods: {
        /**
         * 获取表格数据
         */
        getTableData (page) {
            this.$post('/Freight/lists', {
                page_id: page || this.page.now_page,
                keyword: this.filterHead.keyword,
                template_status: this.filterHead.template_status,
                freight_type: this.filterHead.freight_type,
                sort_field: this.filterData.sort_field,
                sort_type: this.filterData.sort_type
            }, resp=>{
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 编辑模版
         */
        fnEditTemp (row) {
            this.$router.push({
                name: 'goodsFreightEdit',
                query: {
                    template_no: row.template_no
                }
            });
        },

        /**
         * 更改模版状态
         */
        fnChangeStatus (row) {
            this.$post('/Freight/setStatus', {
                template_no: row.template_no,
                template_status: row.template_status
            }, resp=>{
                if(resp.code == 1) {
                    this.getTableData();
                    this.$notify({
                        title: '成功',
                        message: '修改成功',
                        type: 'success'
                    });
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * 删除
         */
        fnRemoveTemp (row) {
            this.$post('/Freight/delete', {
                template_no: row.template_no
            }, resp=>{
                if(resp.code == 1) {
                    this.getTableData();
                    this.$notify({
                        title: '成功',
                        message: '删除成功',
                        type: 'success'
                    });
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * 查看模版配送区域数据
         */
        fnCheckList (row) {
            this.freight_type = row.freight_type == 'weight' ? true : false;
            this.freight_list = row.freight_list;
            this.dialogVisib = !this.dialogVisib;
        },

        /**
         * 数据排序
         */
        handleSortChanged(data) {
            let sortTypeMap = {
                ascending: 'asc',
                descending: 'desc'
            };

            let sortFieldMap = {
                template_sort: 'template_sort'
            };

            if (data.order) {
                this.filterData.sort_field = sortFieldMap[data.prop];
                this.filterData.sort_type = sortTypeMap[data.order];
            } else {
                this.filterData.sort_field = '';
                this.filterData.sort_type = '';
            }

            this.getTableData();
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .marketGoodFreight {
        background-color: #fff;
        padding: 24px 32px 5px 24px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        height: 100%;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 14px;
            .header-left, .header-right {
                display: flex;
                align-items: center;
                flex-wrap: wrap;
            }
            .header-new-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #333333;
                margin-bottom: 10px;
                white-space: nowrap;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                    color: #333333;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
</style>
<style lang="less">
    .goodfreight{
        .el-table__body-wrapper .el-table__body .el-table__row td {
            border-right: none;
        }
        .el-table__header-wrapper .el-table__header .has-gutter tr th {
            border-right: none;
        }
        .el-table .el-table_1_column_6  .cell {
            position: relative;
        }
        .el-range-editor--small .el-range-separator{
            padding: 0 1px !important;
        }
        .el-table__header-wrapper .el-table__header .has-gutter tr th {
            background: #F2F2F2;
            color: #333333;
            font-weight: 500;
        }
        .el-table__header {
            tr > th:last-child {
                background: #F2F2F2 !important;
                color: #333333 !important;
            }
        }
    }
</style>